package com.fundodoo.sfe.common.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class DateUtil {

    private DateUtil(){

    }

    public static String getDateFormat(Date date, String dateFormatType) {
        SimpleDateFormat simformat = new SimpleDateFormat(dateFormatType);
        return simformat.format(date);
    }

    public static String formatCSTTime(String date, String format) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
        Date d = sdf.parse(date);
        return DateUtil.getDateFormat(d, format);
    }


    public static Date strDate(String str, String format) {
        if (null == str || "".equals(str)) {
            return null;
        }
        if (null == format || "".equals(format)) {
            format = "yyyy-MM-dd HH:mm:ss";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        Date date = null;
        try {
            date = sdf.parse(str);
            return date;
        } catch (ParseException e) {

        }
        return null;
    }
    
    public static long getDatePoor(Date endDate, Date nowDate) {
		long nd = 1000 * 24 * 60 * 60;
		long nh = 1000 * 60 * 60;
		long nm = 1000 * 60;
		// long ns = 1000;
		// 获得两个时间的毫秒时间差异
		long diff = endDate.getTime() - nowDate.getTime();
		long day;
		if(diff < 0) {
			day = -1;
		}else {
			day = diff / nd;
		}
		// 计算差多少天
		
//		// 计算差多少小时
//		long hour = diff % nd / nh;
//		// 计算差多少分钟
//		long min = diff % nd % nh / nm;
//		// 计算差多少秒//输出结果
//		// long sec = diff % nd % nh % nm / ns;
//		return day + "天" + hour + "小时" + min + "分钟";
		return day;
	}
    
    public static Date getNextMonth() {
		Date nextMonthToday = null;
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.MONTH, 1);//增加一个月
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			nextMonthToday = df.parse(df.format(calendar.getTime()));
//			System.out.println("增加月份后的日期："+nextMonthToday);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return nextMonthToday;
	}
    
    public static Date getNextAfterMonth(int afterNum) {
		Date nextMonthToday = null;
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.MONTH, afterNum);//增加一个月
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			nextMonthToday = df.parse(df.format(calendar.getTime()));
//			System.out.println("增加月份后的日期："+nextMonthToday);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return nextMonthToday;
	}
}
